﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;

namespace TypeCounter
{
    public class InputCounter : INotifyPropertyChanged
    {
        private long keyboardPressCount;
        private long mouseClickCount;

        /// <summary>
        /// Initializes a new instance of the <see cref="InputCounter"/> class.
        /// </summary>
        public InputCounter()
        {
            VirtualInput.VirtualKeyboard.KeyDown += this.OnVirtualKeyboardKeyDown;
            VirtualInput.VirtualMouse.MouseActivity += this.OnVirtualMouseActivity;

            VirtualInput.VirtualKeyboard.StartInterceptor();
            VirtualInput.VirtualMouse.StartInterceptor();           
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets or sets the keyboard press count.
        /// </summary>
        /// <value>
        /// The keyboard press count.
        /// </value>
        public long KeyboardPressCount
        {
            get
            {
                return this.keyboardPressCount;
            }
            set
            {
                this.keyboardPressCount = value;
                this.RaiseNotifyPropertyChanged();
            }
        }

        /// <summary>
        /// Gets or sets the mouse click count.
        /// </summary>
        /// <value>
        /// The mouse click count.
        /// </value>
        public long MouseClickCount
        {
            get
            {
                return this.mouseClickCount;
            }
            set
            {
                this.mouseClickCount = value;
                this.RaiseNotifyPropertyChanged();
            }
        }

        /// <summary>
        /// Called when keyboard key is down.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="keyEventArgs">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void OnVirtualKeyboardKeyDown(object sender, System.Windows.Forms.KeyEventArgs keyEventArgs)
        {
            this.KeyboardPressCount++;
        }

        /// <summary>
        /// Called when mouse key is down.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="mouseEventArgs"></param>
        private void OnVirtualMouseActivity(object sender, System.Windows.Forms.MouseEventArgs mouseEventArgs)
        {
            if ((mouseEventArgs.Button & (System.Windows.Forms.MouseButtons.Left | System.Windows.Forms.MouseButtons.Middle | System.Windows.Forms.MouseButtons.Right)) != 0)
            {
                this.MouseClickCount++;
            }
        }

        /// <summary>
        /// Raises the notify property changed event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        private void RaiseNotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
